package com.appspot.javametronome.client.view.widget;

import com.appspot.javametronome.client.controller.BpmController;
import com.appspot.javametronome.client.controller.TickController;
import com.appspot.javametronome.client.model.Bpm;
import com.google.gwt.user.client.ui.ToggleButton;

public class PlayButton extends ToggleButton implements GroupableButton {

    private static String upText = "Play", downText = "Stop";
    private ButtonGroup group;
    private boolean selected = false;

    public PlayButton() {
        super(upText, downText);
    }

    @Override
    public void setButtonGroup(ButtonGroup group) {
        this.group = group;
    }

    @Override
    public void onClick() {
        super.onClick();
        if (!selected) {
            group.selectButton(this);
        }
        // User pressed Play
        if (isDown()) {
            Bpm bpm = BpmController.instance.getBpm();
            bpm.setBpm(BpmController.instance.getView().getCurrentBpm());
            TickController.instance.start(BpmController.instance.getBpm());
        }
        // User pressed Stop
        else {
            TickController.instance.stop();
        }
    }

    @Override
    public void setSelected() {
        if (!selected) {
            selected = true;
        }
    }

    @Override
    public void setUnselected() {
        if (selected) {
            selected = false;
            this.setDown(false);
        }
    }
}
